package com.charm.codes.imageLoad;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.charm.codes.imageLoad.transformation.GlideBlurformation;

import java.io.File;
import java.util.concurrent.ExecutionException;


/**
 * @author ouyang
 * @date 2017/12/11
 */

public class XGlide {
    /**
     * 单例
     */
    private static volatile XGlide mInstance;

    /**
     *
     */
    Context mContext;
    /**
     * 图片URL
     */
    String imageUrl;

    /**
     *
     */
    int placeholderResId = -1;
    /**
     *
     */
    int errorResId = -1;

    /**
     *
     */
    int blurRadius = 100;
    /**
     *
     */
    ImageView imageView;

    private XGlide() {
    }

    public static XGlide getInstance() {
        if (mInstance == null) {
            synchronized (XGlide.class) {
                if (mInstance == null) {
                    mInstance = new XGlide();
                    return mInstance;
                }
            }
        }
        return mInstance;
    }


    private GlideRequests getGlideRequests(Context context) {
        return GlideApp.with(context);
    }

    /**
     * @param context
     * @return
     */
    public XGlide with(Context context) {
        this.mContext = context;
        return mInstance;
    }

    /**
     * @param url
     * @return
     */
    public XGlide load(String url) {
        this.imageUrl = url;
        return mInstance;
    }

    /**
     * @param placeholderResId
     * @return
     */
    public XGlide placeholder(int placeholderResId) {
        this.placeholderResId = placeholderResId;
        return mInstance;
    }

    /**
     * @param errorResId
     * @return
     */
    public XGlide error(int errorResId) {
        this.errorResId = errorResId;
        return mInstance;
    }


    public XGlide blurRadius(int blurRadius) {
        this.blurRadius = blurRadius;
        return mInstance;
    }

    /**
     * @param imageView
     * @return
     */
    public XGlide into(ImageView imageView) {
        this.imageView = imageView;
        return mInstance;
    }


    /**
     * 普通加载
     */
    public void displayImage() {
        GlideRequests requests = getGlideRequests(mContext);
        if (TextUtils.isEmpty(imageUrl)) {
            return;
        }
        GlideRequest load = requests.load(imageUrl);
        if (placeholderResId != -1) {
            load.placeholder(placeholderResId);
        }
        if (errorResId != -1) {
            load.error(errorResId);
        }
        if (imageView == null) {
            return;
        }
        load.into(imageView);
    }


    /**
     * 圆形
     */
    public void displayCircleImage() {

        GlideRequests requests = getGlideRequests(mContext);
        if (TextUtils.isEmpty(imageUrl)) {
            return;
        }
        GlideRequest load = requests.load(imageUrl);
        load.transform(new CircleCrop());
        if (placeholderResId != -1) {
            load.placeholder(placeholderResId);
        }
        if (errorResId != -1) {
            load.error(errorResId);
        }
        if (imageView == null) {
            return;
        }
        load.into(imageView);
    }

    /**
     * 高斯模糊
     */
    public void displayBlurImage() {

        GlideRequests requests = getGlideRequests(mContext);
        if (TextUtils.isEmpty(imageUrl)) {
            return;
        }
        GlideRequest load = requests.load(imageUrl);
        load.transform(new GlideBlurformation(mContext, blurRadius));
        if (placeholderResId != -1) {
            load.placeholder(placeholderResId);
        }
        if (errorResId != -1) {
            load.error(errorResId);
        }
        if (imageView == null) {
            return;
        }
        load.into(imageView);
    }

    /**
     * 在一个imageView里面异步展示一个图片
     *
     * @param uri
     * @param imageView
     */
    public void displayAsnyImage(Context context, String uri, ImageView imageView, int placeholderResId) {

        GlideRequests requests = getGlideRequests(mContext);
        if (TextUtils.isEmpty(imageUrl)) {
            return;
        }
        GlideRequest load = requests.load(imageUrl);
        load.dontAnimate();
        if (placeholderResId != -1) {
            load.placeholder(placeholderResId);
        }
        if (errorResId != -1) {
            load.error(errorResId);
        }
        load.diskCacheStrategy(DiskCacheStrategy.ALL);
        if (imageView == null) {
            return;
        }
        load.into(imageView);
    }

    public void setTarget(Context context, String uri, @NonNull Target target) {

        GlideRequests requests = getGlideRequests(mContext);

        GlideRequest bitmapRequest = requests.asBitmap();
        if (TextUtils.isEmpty(imageUrl)) {
            return;
        }
        GlideRequest load = bitmapRequest.load(imageUrl);
        load.into(target);
    }

    /**
     * 获取磁盘缓存
     *
     * @return
     */
    public DiskCache getDiskCache() {
        return XGlide.getInstance().getDiskCache();
    }

    /**
     * 获取内存缓存
     *
     * @return
     */
    public MemoryCache getMemoryCache() {
        return XGlide.getInstance().getMemoryCache();
    }

    /**
     * 注意！！！该方法必须在子线程中执行
     * 清除硬盘缓存
     */
    public void cleanDiskCache(final Context context) {
        Glide.get(context).clearDiskCache();
    }

    /**
     * 清除内存缓存
     */
    public void cleanMemoryCache(Context context) {
        Glide.get(context).clearMemory();
    }

    /**
     * 内存和硬盘双清
     */
    public void cleanDoubleCache(Context context) {
        cleanDiskCache(context);
        cleanMemoryCache(context);
    }

    /**
     * 恢复请求，一般在停止滚动的时候调用
     *
     * @param context
     */
    public void resumeRequests(Context context) {
        Glide.with(context).resumeRequests();
    }

    /**
     * 暂停请求，一般在滚动的时候调用
     *
     * @param context
     */
    public void pauseRequests(Context context) {
        Glide.with(context).pauseRequests();
    }

    /**
     * 根据图片的网络地址，拿到使用 Glide 进行缓存后的图片缓存地址
     * 注意！！！ 该方法要在子线程中调用，否则会出错
     *
     * @param imageUrl 图片的网络地址
     * @return 图片的缓存地址
     */
    public static String getImagePathFromCache(String imageUrl, Context context) {

        FutureTarget<File> futureTarget = Glide.with(context)
                .load(imageUrl)
                .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
        File cacheFile;
        try {
            cacheFile = futureTarget.get();
            return cacheFile.getAbsolutePath();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用于监听异步加载图片的过程
     */
    public interface ImageLoadingListener {
        void onSuccess();
        void onError();
    }

}
